package trunk.src.levelUp.Library;

import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: Станислав
 * Date: 18.01.14
 * Time: 22:31
 * To change this template use File | Settings | File Templates.
 */
public class FileMaster {
    public static List<String> getFileContent(String path) {
        List<String> toSend = new LinkedList<String>();
        try {
            FileInputStream fin = null;
            Scanner sc = null;
            try {
                fin = new FileInputStream(path);
                sc = new Scanner(fin);
                while (sc.hasNextLine()) {
                    toSend.add(sc.nextLine());
                }
            } finally {
                if (fin != null) {
                    fin.close();
                }
                if (sc != null) {
                    sc.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toSend;
    }

    public static boolean writeToFile(List<String> toWrite, String path) {
        try {
            FileWriter fileWritter = null;
            try {
                fileWritter = new FileWriter(path, false);
                Iterator<String> iter = toWrite.iterator();
                while (iter.hasNext()) {
                    fileWritter.write(iter.next());
                    fileWritter.write("\r\n");
                }
                return true;
            } finally {
                if (fileWritter != null) {
                    fileWritter.flush();
                    fileWritter.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

